<template>
  <div class="main-container">
      <el-button @click="handleBatchDelete">批量删除</el-button>

        <el-table
          :data="videoData"
          border
          :header-cell-style="{ color: 'black', 'background-color': '#f5f7f9','text-align': 'center' }"
          @selection-change="handleSelectionChange"
          >
          <!-- 添加多选框列 -->
          <el-table-column type="selection" width="55"></el-table-column>
        
          <!-- 定义列 -->
          <el-table-column
            style="width: auto;"
            v-for="item in videoDataLabel"
            :key="item.prop"
            :prop="item.prop"
            :label="item.label"
            show-overflow-tooltip
            :width="item.width ? item.width : 130"
            :sortable="item.sortable ? item.sortable : false"
          >
          <template #default="{ row }" v-if="item.prop === 'videoUrl'">
              <div style="display: flex; justify-content: center; align-items: center;">
                <img 
                :src="row.videoUrl+hoZhui" 
                alt="视频" 
                style="width: 50px; height: 30px; object-fit: cover;">
              </div>
            </template>

          <template #default="{ row }" v-else-if="item.prop === 'imageUrl'">
              <div style="display: flex; justify-content: center; align-items: center;">
                <img :src="row.imageUrl" alt="图片" style="width: 80px; height: 30px; object-fit: cover;">
              </div>
            </template>
            <template #default="{ row }" v-else>
              <div style="text-align: center;">
                {{ row[item.prop] }}
              </div>
            </template>
          </el-table-column>
          <!-- 操作列 -->
          <el-table-column label="操作">
            <template #default="{ row }">
              <div class="action-column">
                <el-button @click="handleDelete(row)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
 
       <!-- 分页组件 -->
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          v-model:current-page="pagination.pageNo"
          :current-page="pagination.pageNo"
          :page-size="pagination.pageSize"
          layout="total,prev, pager, next"
          :total="pagination.total"
        >
        </el-pagination>
      </div>
    </div>
  </template>
  
<script setup>
  import { ref,onMounted } from 'vue';
  import { getVideoListAPI } from '../../../../api/admin/content'
  import { delVideoAPI,delVideoSAPI } from '../../../../api/admin.js'
  import dayjs from 'dayjs';
  const hoZhui=ref("?x-oss-process=video/snapshot,t_0,f_jpg")
// 声明 selectedRows 变量
const selectedRows = ref([]);
  // 表格列配置
  const videoDataLabel = ref([
    {
      prop: 'id',
      label: '视频id',
      width: 100,
      sortable: true
    },
    {
      prop: 'title',
      label: '标题',
    },
    {
      prop: 'likes',
      label: '喜欢',
      width: 80,
    },
    {
      prop: 'imageUrl',
      label: '图片',
      width: 150,
    },
    {
      prop: 'videoUrl',
      label: '视频',
      width: 150,
    },
    {
      prop: 'authorId',
      label: '作者id',
      width: 100,
    },
    {
      prop: 'content',
      label: '内容',
    },
    {
      prop: 'createTime',
      label: '创建时间',
      sortable: true,
      width: 150,
    },
    {
      prop: 'updateTime',
      label: '修改时间',
      width: 150,
      sortable: true
    },
  ]);
  // 表格数据
  const videoData = ref([]);
  // 分页相关数据封装
  const pagination = ref({
  pageNo: 1,
  pageSize: 10,
  total: 0,
});
  
  // 删除按钮点击事件
const handleDelete = async(row) => {
  const response = await delVideoAPI({videoId:row.id});
      // console.log(response.data);
      await fetchVideoList();  
};
// 多选框选中状态改变时触发
const handleSelectionChange = (rows) => {
    selectedRows.value = rows;
  };
  // 批量删除按钮点击事件
 const handleBatchDelete = async () => {
    if (selectedRows.value.length === 0) {
      console.log('请选择要删除的戏曲');
      return;
    }
    const ids = selectedRows.value.map(row => row.id.toString()).join(',');
    try {
      const response = await delVideoSAPI({ids});
      if (response.data.code === 0) {
        // 从表格数据中移除已删除的行
        // videoData.value = videoData.value.filter(item =>!ids.includes(item.id));
    await fetchVideoList();
        console.log('批量删除成功');
      } else {
        console.error('批量删除失败:', response.message);
      }
    } catch (error) {
      console.error('批量删除失败:', error);
    }
  };
   // 每页显示数量改变时触发
   const handleSizeChange = async (newSize) => {
    pagination.value.pageSize = newSize;
    pagination.value.pageNo = 1;
    await fetchVideoList();
 };

// 当前页码改变时触发
const handleCurrentChange = async (newPage) => {
    pagination.value.pageNo = newPage;
    await fetchVideoList();
  };
// 获取戏曲列表
const fetchVideoList = async () => {
  try {
    let param = {
      pageNo: pagination.value.pageNo,
      pageSize: pagination.value.pageSize
    };
    const response = await getVideoListAPI(param);
    // console.log(response.data);
    const formattedData = response.data.data.list.map(item => {
        return {
          ...item,
          createTime: dayjs(item.createTime).format('YYYY-MM-DD HH:mm'),
          updateTime: dayjs(item.updateTime).format('YYYY-MM-DD HH:mm'),
        };
      });
      videoData.value = formattedData;
    //videoData.value = response.data.data.list;
    pagination.value.total = response.data.data.total;
   } catch (error) {
    console.error('获取戏曲列表失败:', error);
  }
};
 // 组件挂载时获取数据
 onMounted(async () => {
    await fetchVideoList();
});
</script>
  
<style lang="less" scoped>
.main-container{
  height: calc(100% - 64px);
  // height: 100%;
  // background-color: aqua;
}
.el-table {
    // overflow-y: scroll;
    height: 100%;
    // background-color: red;
    .action-column {
      display: flex;
      flex-wrap: nowrap;
      /* 确保按钮不换行 */
      white-space: nowrap;
      /* 防止按钮内的文本换行 */
    }
}
.pagination-container {
  display: flex;
  // background-color: rgb(55, 224, 21);
  justify-content: center;
  // margin-top: 20px;
}
</style>